package com.vctgo.evidence.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vctgo.common.core.utils.StringUtils;
import com.vctgo.evidence.domain.TbEvi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vctgo.evidence.mapper.TbEvidenceMapper;
import com.vctgo.evidence.domain.TbEvidence;
import com.vctgo.evidence.service.ITbEvidenceService;
/**
 * 凭证列表Service业务层处理
 *
 * @author vctgo
 * @date 2024-12-15
 */
@Service
public class TbEvidenceServiceImpl implements ITbEvidenceService
{
    @Autowired
    private TbEvidenceMapper tbEvidenceMapper;

    /**
     * 查询凭证列表
     *
     * @param id 凭证列表主键
     * @return 凭证列表
     */
    @Override
    public TbEvidence selectTbEvidenceById(Long id)
    {
        return tbEvidenceMapper.selectEvidenceById(id);
    }

    /**
     * 查询凭证列表列表
     *
     * @param tbEvidence 凭证列表
     * @return 凭证列表
     */
    @Override
    public List<TbEvidence> selectTbEvidenceList(TbEvidence tbEvidence)
    {
        return tbEvidenceMapper.selectTbEvidenceList(tbEvidence);
    }


    /**
     * 借贷平衡校验
     */
    private boolean isLedgerBalanced(TbEvidence tbEvidence) {
        // 在这里实现具体的借贷平衡校验逻辑
        Double creditorMoney= 0.0;
        Double debtorMoney =0.0;
        for (TbEvi tbEvi : tbEvidence.getTbEviList()) {
            creditorMoney += tbEvi.getCreditorMoney();
            debtorMoney += tbEvi.getDebtorMoney();
        }
        if (!creditorMoney.equals(debtorMoney)){
            return false;
        }
        // 返回true表示校验通过，false表示校验失败
        // 示例中总是返回true
        return true;
    }



    /**
     * 新增凭证列表
     *
     * @param tbEvidence 凭证列表
     * @return 结果
     */

    @Override
    public int insertTbEvidence(TbEvidence tbEvidence)
    {
        // 执行借贷平衡校验
        if (!isLedgerBalanced(tbEvidence)) {
            // 或者抛出自定义异常
            throw new RuntimeException("借贷不平衡");
        }

        ArrayList<String> accountSubject = new ArrayList<>();
        for (TbEvi tbEvi : tbEvidence.getTbEviList()) {
            String accountingSubject = tbEvi.getAccountingSubject();
            accountSubject.add(accountingSubject);
        }
        if (accountSubject.isEmpty()){
            throw new RuntimeException("添加的科目不存在");
        }
        int rows = tbEvidenceMapper.insertEvidence(tbEvidence);
        insertTbEvi(tbEvidence);
        return rows;
    }



    /**
     * 修改凭证列表
     *
     * @param tbEvidence 凭证列表
     * @return 结果
     */
    @Override
    public int updateTbEvidence(TbEvidence tbEvidence)
    {
        tbEvidenceMapper.deleteTbEviByEvidenceId(tbEvidence.getId());
        insertTbEvi(tbEvidence);
        return tbEvidenceMapper.updateById(tbEvidence);
    }

    /**
     * 批量删除凭证列表
     *
     * @param ids 需要删除的凭证列表主键
     * @return 结果
     */
    @Override
    public int deleteTbEvidenceByIds(Long[] ids)
    {
        tbEvidenceMapper.deleteEviByEvidenceIds(ids);
        return tbEvidenceMapper.deleteTbEvidenceByIds(ids);
    }

    /**
     * 删除凭证列表信息
     *
     * @param id 凭证列表主键
     * @return 结果
     */
    @Override
    public int deleteTbEvidenceById(Long id)
    {
        tbEvidenceMapper.deleteEviByEvidenceId(id);
        return tbEvidenceMapper.deleteById(id);
    }

    @Override
    public int deleteEviByEvidenceIds(Long[] ids) {
        return 0;
    }

    /**批量添加*/
    private void insertTbEvi(TbEvidence tbEvidence) {
        List<TbEvi> tbEviList = tbEvidence.getTbEviList();
        Long id = tbEvidence.getId();
        if (StringUtils.isNotNull(tbEviList)){
            ArrayList<TbEvi> tbEvis = new ArrayList<>();
            for (TbEvi tbEvi : tbEviList) {
                tbEvi.setEvidentId(id);
                tbEvi.setTenantId(1L);
                tbEvis.add(tbEvi);
            }
            if (tbEvis.size()>0){
                tbEvidenceMapper.batchTbEvi(tbEvis);
            }
        }
    }
}
